package cc.chenwenxi.tts;

import java.io.BufferedInputStream;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import cn.hutool.core.util.URLUtil;
import kits.tts.baidu.TokenBaidu;
import kits.tts.baidu.TtsEntity;
import kits.tts.baidu.TtsKit;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Data
@Slf4j
@NoArgsConstructor
@AllArgsConstructor
public class TtsBaiduKit {
	private String say;
	
	public BufferedInputStream getInput() {
		try {
			InputStream stream = URLUtil.getStream(new URL(this.getSay()));
			BufferedInputStream buffer = new BufferedInputStream(stream);
			return buffer;
		} catch (MalformedURLException e) {
			log.error("转语音异常",e);
		}
		return null;
	}
	
	private String getSay() {
		TtsKit test = new TtsKit();
		// 1.获取token
		TokenBaidu token = test.getToken();
		// 2.测试语音合成
		TtsEntity tts = new TtsEntity(say, token.getAccess_token());
		return test.getSayMp3Url(tts);
	}
}
